package org.aceor.funny.common.http.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by lxue on 15/11/19.
 */
public class IOUtils {
    public static final int DEFAULT_SIZE = 1024;
    public static byte[] toByteArray(InputStream is) throws IOException {
        return toByteArray(is, DEFAULT_SIZE);
    }

    public static byte[] toByteArray(InputStream is, int size) throws IOException {
        if(size < 0)
            throw new IllegalArgumentException("size cannot less than 0.");
        if(null == is)
            throw new NullPointerException("InputStream cannot be null.");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[size];
        int n = 0;
        while(-1 != (n = is.read(buffer))){
            baos.write(buffer, 0, n);
        }
        return baos.toByteArray();
    }
}

